package com.hello.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hello.dto.PetcardDTO;
import com.hello.dto.UserLoginDTO;
import com.hello.entity.Petcard;
import com.hello.mapper.PetcardMapper;
import com.hello.service.IPetCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.security.auth.login.AccountNotFoundException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class PetCardServiceImpl extends ServiceImpl<PetcardMapper, Petcard> implements IPetCardService {
    @Override

    public boolean addPetCard(PetcardDTO petcardDTO) {
        Petcard petcard = new Petcard();
        BeanUtils.copyProperties(petcardDTO,petcard);
        //获取宠物id最大值max_pet_id将当前宠物id设置为最大值+1
        Long maxPetId = baseMapper.getMaxId();
        petcard.setId(maxPetId+1);
        int num = baseMapper.insert(petcard);
        return num>0?true:false;
    }

    @Override
    public boolean delPetCardById(Long id) {
        int num = baseMapper.deleteById(id);
        return num>0?true:false;
    }

    @Override
    public List<PetcardDTO> queryPetCardList(Long id) {
        List<PetcardDTO> petcardDTOS = new ArrayList<>();

        List<Petcard> list = lambdaQuery().list();
        for(Petcard petcard:list){
            if(petcard.getId() == id){
                PetcardDTO petcardDTO = new PetcardDTO();
                BeanUtils.copyProperties(petcard,petcardDTO);
                petcardDTOS.add(petcardDTO);
            }

        }
        return petcardDTOS;
    }
    @Override
    public List<PetcardDTO> myPetCard(Long user_id){
        List<PetcardDTO> petcardDTOList = new ArrayList<>();
        QueryWrapper<Petcard> wrapper = new QueryWrapper<Petcard>()
                .select("*")
                .eq("user_id",user_id);
        List<Petcard> petcardList = baseMapper.selectList(wrapper);
        for(Petcard petcard:petcardList){
            PetcardDTO petcardDTO = new PetcardDTO();
            BeanUtils.copyProperties(petcard,petcardDTO);
            petcardDTOList.add(petcardDTO);
        }
        return petcardDTOList;
    }

}
